package com.platform.modules.mall.controller.associtag;

import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.entity.MallActivityEntity;
import com.platform.modules.mall.entity.MallAssociTagEntity;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.service.MallActivityService;
import com.platform.modules.mall.service.MallGoodsToActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("shopsGoodsTag")
public class ShopsGoodsTag extends AssociTagDataChangeAbs<MallGoodsEntity>{

    @Resource
    private MallGoodsToActivityService goodsToActivityService;
    @Resource
    private MallActivityService activityService;

    @Override
    public boolean add(String associId, MallGoodsEntity goods) {
        if(!addVerify(associId,goods.getId())){
            return false;
        }
        MallAssociTagEntity entity = new MallAssociTagEntity();
        if(goods.getGoodsType() == 1){
            // 卡券
            entity.setAssociId(associId);
            entity.setTitle(goods.getName());
            entity.setType(MallAssociTagEntity.Type.QUAN.getValue());
            entity.setOtherId(goods.getId());
            List<MallGoodsToActivityEntity> goodsToActivityEntitys = goodsToActivityService.lambdaQuery()
                    .select(MallGoodsToActivityEntity::getActivityId,MallGoodsToActivityEntity::getId)
                    .eq(MallGoodsToActivityEntity::getGoodsId,goods.getId())
                    .list();
            if(goodsToActivityEntitys != null && !goodsToActivityEntitys.isEmpty()){
                // 活动卡券
                MallActivityEntity activityEntity = activityService.lambdaQuery()
                        .select(MallActivityEntity::getName,MallActivityEntity::getId,MallActivityEntity::getIsShow)
                        .eq(MallActivityEntity::getId,goodsToActivityEntitys.get(0).getActivityId())
                        .one();
                if(activityEntity != null && activityEntity.getIsShow().equals(1)){
                    entity.setDescribe(activityEntity.getName());
                    entity.setType(MallAssociTagEntity.Type.QIANG.getValue());
                    entity.setOtherId1(activityEntity.getId());
                }
            }
            return tagService.add(entity);
        }
        return false;
    }

    @Override
    public boolean update(String otherId, MallGoodsEntity entity) {
        if(StringUtils.isBlank(otherId)){
            return true;
        }
        return tagService.lambdaUpdate()
                .set(MallAssociTagEntity::getTitle, entity.getName())
                .eq(MallAssociTagEntity::getOtherId, otherId)
                .update();
    }

    @Override
    public boolean associAdd(MallActivityEntity entity, String... otherId) {
        return true;
    }

    @Override
    public boolean associUpdate(String otherId1, MallGoodsEntity entity) {
        return true;
    }

    @Override
    public boolean associDelate(String... otherId1) {
        return true;
    }
}
